/*
 * NativeCallbacksImplementation.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.delegate;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeGenerator;
import com.cute4j.tools.ng.Utils;

/**
 * Native Callbacks Implementation.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
class NativeCallbacksImplementation extends NativeImplementationBlock {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeCallbacksDeclaration object.
   *
   * @param nativeFile the abstract native file
   * @param text the text
   * @param javaClass the java class
   */
  public NativeCallbacksImplementation(AbstractNativeFile nativeFile,
                                       String text, JavaClass javaClass) {
    super(nativeFile, text, javaClass);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void store(PrintWriter pw) throws IOException {
    //super.store(pw);
    //String delegateObjectName = Utils.getDelegateObjectName(getJavaClass());
    ArrayList textMethods = getMethodImplementationsFromText();
    JavaMethod[] methods = getJavaClass().getMethods();
    boolean[] overriddenMethods = new boolean[methods.length];
    boolean labeled = false;
    for (int i = 0; i < methods.length; i++) {
      JavaMethod method = methods[i];
      overriddenMethods[i] = (!method.isNative() && !method.isAbstract() &&
                              (method.getName().charAt(0) == '_') &&
                              !"true".equals( //$NON-NLS-1$
                              Utils.getNativeTagParameter(method,
                                                          "overridden")));
    }
    for (int i = 0; i < methods.length; i++) {
      JavaMethod method = methods[i];
      if (overriddenMethods[i]) {
        if (!labeled) {
          Utils.printBlockComment(pw, Utils.getDelegateName(getJavaClass()) +
                                  " - Native Callbacks.");
          labeled = true;
        }
        String methodImplementation =
                                 Utils.getCallbackMethodImplementation(method);
        int mdi = Utils.getMethodImplementationIndex(textMethods,
                                                     methodImplementation);
        if (mdi < 0) {
          pw.println(Utils.getDelegateMethodComment(method));
          pw.println(methodImplementation);
        } else {
          pw.println(textMethods.get(mdi));
          textMethods.remove(mdi);
        }
      }
    }
    String newLine = NativeGenerator.getInstance().getNewLine();
    String methodStart = " */" + newLine;
    int methodStartLength = methodStart.length();
    String methodEnd = newLine + "}";
    int methodEndLength = methodEnd.length();
    for (int i = 0; i < textMethods.size(); i++) {
      StringBuffer methodText = new StringBuffer((String)textMethods.get(i));
      int startIndex = methodText.indexOf(methodStart);
      int endIndex = methodText.indexOf(methodEnd);
      if (methodText.charAt(startIndex + methodStartLength) == '/') {
        pw.println(methodText);
      } else {
        methodText = methodText.insert(endIndex + methodEndLength, "*/");
        methodText = methodText.insert(startIndex + methodStartLength, "/*");
        pw.println(methodText);
      }
    }
  }

}
